#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class App {
	int cnt;
	vector<int> head, next, to, weight, dgree;
	vector<int> flow, dp;

	void addedge(int u, int v, int w)
	{
		next[cnt] = head[u];
		to[cnt] = v;
		weight[cnt] = w;
		head[u] = cnt++;
	}

	void dfs1(int u, int p)
	{
		for (int e = head[u], v; e; e = next[e]) {
			v = to[e];
			if (v != p) {
				dfs1(v, u);
				if (dgree[v] == 1) {
					flow[u] += weight[e];
				} else {
					flow[u] += min(flow[v], weight[e]);
				}
			}
		}
	}

	void dfs2(int u, int p)
	{
		for (int e = head[u], v, u_out; e; e = next[e]) {
			v = to[e];
			if (v != p) {
				if (dgree[u] == 1) {
					dp[v] = flow[v] + weight[e];
				} else {
					u_out = flow[u] - min(flow[v], weight[e]);
					dp[v] = flow[v] + min(u_out, weight[e]);
				}
				dfs2(v, u);
			}
		}
	}

	void dfs3(int u, int p)
	{
		for (int e = head[u], v, u_out; e; e = next[e]) {
			v = to[e];
			if (v != p) {
				if (dgree[u] == 1) {
					flow[v] += weight[e];
				} else {
					u_out = flow[u] - min(flow[v], weight[e]);
					flow[v] += min(u_out, weight[e]);
				}
				dfs3(v, u);
			}
		}
	}

    public:
	App(int n)
		: cnt(1), head(n + 1), next(n << 1, 0), to(n << 1, 0), weight(n << 1, 0), dgree(n + 1, 0), /**/
		  flow(n + 1, 0), dp(n + 1, 0)
	{
		for (int i = 0, u, v, w; i < n - 1; i++) {
			cin >> u >> v >> w;
			dgree[u]++;
			dgree[v]++;
			addedge(u, v, w);
			addedge(v, u, w);
		}
		dfs1(1, 0);
		dp[1] = flow[1];
		dfs3(1, 0);
		cout << *max_element(flow.begin() + 1, flow.end()) << '\n';
	}
};

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);

	int t = 0;
	cin >> t;
	while (t--) {
		int n = 0;
		cin >> n;
		App app(n);
	}
	return 0;
}